﻿using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using NUnit.Framework;
using NUnit.Framework.Constraints;
using NUnit.Framework.SyntaxHelpers;
using Yaml.Presentation.Tokens;
using Yaml.Presentation.Parsing;

namespace Yaml.Tests.Presentation.ScannerTests {
    [TestFixture]
    public class EmptyDocument {
        [Test]
        public void EmptyUTF8Document() {
            Scanner scanner = new Scanner(Helper.CreateReader("", Encoding.UTF8));
            Assert.That(scanner.State.Done, Is.False);
            Token t = scanner.GetToken();
            Assert.That(t.ToString(), Is.EqualTo("#<StreamStartToken encoding='utf-8' start={0:0,0} end={0:0,0}>"));
            t = scanner.GetToken();
            Assert.That(t.ToString(), Is.EqualTo("#<StreamEndToken start={0:0,0} end={0:0,0}>"));
        }

        [Test]
        public void EmptyLittleEndianDocument() {
            Scanner scanner = new Scanner(Helper.CreateReader("", Encoding.Unicode));
            Assert.That(scanner.State.Done, Is.False);
            Token t = scanner.GetToken();
            Assert.That(t.ToString(), Is.EqualTo("#<StreamStartToken encoding='utf-16-le' start={0:0,0} end={0:0,0}>"));
            t = scanner.GetToken();
            Assert.That(t.ToString(), Is.EqualTo("#<StreamEndToken start={0:0,0} end={0:0,0}>"));
        }

        [Test]
        public void EmptyBigEndianDocument() {
            Scanner scanner = new Scanner(Helper.CreateReader("", Encoding.BigEndianUnicode));
            Assert.That(scanner.State.Done, Is.False);
            Token t = scanner.GetToken();
            Assert.That(t.ToString(), Is.EqualTo("#<StreamStartToken encoding='utf-16-be' start={0:0,0} end={0:0,0}>"));
            t = scanner.GetToken();
            Assert.That(t.ToString(), Is.EqualTo("#<StreamEndToken start={0:0,0} end={0:0,0}>"));
        }

        [Test]
        public void WhiteSpaceOnlyDocument() {
            Scanner scanner = new Scanner(Helper.CreateReader("           \n           \r\n          ", Encoding.BigEndianUnicode));
            Assert.That(scanner.State.Done, Is.False);
            Token t = scanner.GetToken();
            Assert.That(t.ToString(), Is.EqualTo("#<StreamStartToken encoding='utf-16-be' start={0:0,0} end={0:0,0}>"));
            t = scanner.GetToken();
            Assert.That(t.ToString(), Is.EqualTo("#<StreamEndToken start={35:2,10} end={35:2,10}>"));
        }

        [Test]
        public void CommentOnlyDocument() {
            Scanner scanner = new Scanner(Helper.CreateReader("# Some comments including \t (tab) and £$%!!%YAML", Encoding.BigEndianUnicode));
            Assert.That(scanner.State.Done, Is.False);
            Token t = scanner.GetToken();
            Assert.That(t.ToString(), Is.EqualTo("#<StreamStartToken encoding='utf-16-be' start={0:0,0} end={0:0,0}>"));
            t = scanner.GetToken();
            Assert.That(t.ToString(), Is.EqualTo("#<StreamEndToken start={48:0,48} end={48:0,48}>"));
        }

        [Test]
        public void WhiteSpaceAndCommentsDocument() {
            Scanner scanner = new Scanner(Helper.CreateReader("         #d\n#1234567890\r\n    #%YAML", Encoding.BigEndianUnicode));
            Assert.That(scanner.State.Done, Is.False);
            Token t = scanner.GetToken();
            Assert.That(t.ToString(), Is.EqualTo("#<StreamStartToken encoding='utf-16-be' start={0:0,0} end={0:0,0}>"));
            t = scanner.GetToken();
            Assert.That(t.ToString(), Is.EqualTo("#<StreamEndToken start={35:2,10} end={35:2,10}>"));
        }
    }
}
